package com.threeti.mecool.common.resource.core;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.springframework.core.io.Resource;

import com.threeti.mecool.common.resource.Content;
import com.threeti.mecool.common.resource.ContentRepo;
import com.threeti.mecool.common.resource.util.FileUtil;


public class ContentRepoLocalAdapter implements ContentRepo {// TODO jay:more meaningful exception

	@Override
	public void setCredentials(String username, String password) {
	}

	@Override
	public void setCredentials(String username, String password, String domain, String workstation) {
	}

	@Override
	public List<Content> list(String location) throws IOException {
		return null;
	}

	@Override
	public List<Content> list(String location, int depth) throws IOException {
		return null;
	}

	@Override
	public List<Content> patch(String location, Map<QName, String> addProps) throws IOException {
		return null;
	}

	@Override
	public List<Content> patch(String location, Map<QName, String> addProps, List<QName> removeProps) throws IOException {
		return null;
	}

	@Override
	public Content get(String location) throws IOException {
		Validate.notEmpty(location, "内容资源的路径不能为空");
		Validate.isTrue(FileUtil.isFileURLBased(location), "未提供文件的协议信息，获取 本地内容资源 仅支持标准的file:协议");
		Resource rs = FileUtil.getResourceLoader().getResource(location);
		
		return ContentFactory.makeContent(rs);
	}

	@Override
	public Content get(String location, Map<String, String> headers) throws IOException {
		return null;
	}

	@Override
	public void put(String location, byte[] data) throws IOException {
		Validate.notEmpty(location, "上传的文件名不能为空");
		Validate.isTrue(data != null && data.length !=0, "上传内容不能为空");
		Validate.isTrue(FileUtil.isFileURLBased(location), "未提供文件的协议信息，本地文件上传 仅支持标准的file:协议");
		Validate.isTrue(FileUtil.isFile(location), "文件上传的目的URI必须也是一个文件");
		
		String directory = FileUtil.getFileDirectory(location);
		createDirectory(directory);
		
		File tagetFile = FileUtil.getFile(location);
		
		OutputStream out = null;
		try {
			IOUtils.write(data, out = new BufferedOutputStream(new FileOutputStream(tagetFile)));
		} finally {
			IOUtils.closeQuietly(out);
		}
		
	}

	@Override
	/** 默认地，资源上传完毕后，输入流将被自动关闭 */
	public void put(String location, InputStream dataStream) throws IOException {
		put(location, dataStream, true);
	}

	@Override
	public void put(String location, InputStream dataStream, boolean isStreamCloseQuietly) throws IOException {
		try {
			put(location, IOUtils.toByteArray(dataStream));
		} finally {
			if (isStreamCloseQuietly) IOUtils.closeQuietly(dataStream);
		}
	}

	@Override
	public void put(String location, byte[] data, String contentType) throws IOException {
	}

	@Override
	public void put(String location, InputStream dataStream, String contentType) throws IOException {
	}

	@Override
	public void put(String location, InputStream dataStream, String contentType, boolean expectContinue)
			throws IOException {
	}

	@Override
	public void put(String location, InputStream dataStream, Map<String, String> headers) throws IOException {
	}

	@Override
	public void delete(String location) throws IOException {
		Validate.notEmpty(location, "内容资源的路径不能为空");
		Validate.isTrue(FileUtil.isFileURLBased(location), "未提供文件的协议信息，获取 本地内容资源 仅支持标准的file:协议");
		Resource rs = FileUtil.getResourceLoader().getResource(location);
		rs.getFile().delete();//TODO jay:force cascade delete
	}

	@Override
	public void createDirectory(String location) throws IOException {
		Validate.notEmpty(location, "文件夹创建路径不能为空");
		Validate.isTrue(FileUtil.isFileURLBased(location), "未提供文件夹的协议信息，本地文件夹创建仅支持标准的file:协议，");
		Validate.isTrue(!FileUtil.isFile(location), "不支持创建含有.的文件夹名");

		String[] pathTree = FileUtil.getFileURLPath(location).split("/");
		String parentPath = "";
		if ("".equals(pathTree[0])) {//是否含有根路径信息
			parentPath = "/";
		}
		
		for (int i = 0; i < pathTree.length; i++) {
			if (StringUtils.isNotEmpty(pathTree[i])) {
				String workingPath = parentPath + pathTree[i] + "/";

				if (!FileUtil.exists(location)) {
					new File(workingPath).mkdir();//will ignore i.e. c:,e:... etc.
				}
				parentPath = workingPath;
			}
		}
	}

	@Override
	public void move(String sourcelocation, String destinationlocation) throws IOException {
	}

	@Override
	public void copy(String sourcelocation, String destinationlocation) throws IOException {
	}

	@Override
	public boolean exists(String location) throws IOException {
		return FileUtil.exists(location);
	}

	@Override
	public String lock(String location) throws IOException {
		return null;
	}

	@Override
	public void unlock(String location, String token) throws IOException {
	}

	@Override
	public void enableCompression() {
	}

	@Override
	public void disableCompression() {
	}

	@Override
	public void enablePreemptiveAuthentication(String hostname) {
	}

	@Override
	public void disablePreemptiveAuthentication() {
	}
	
}
